/**
 * 基础菜单 商品管理
 */
<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 搜索筛选 -->
    <el-form :inline="true" :model="formInline" class="user-search">
      <el-form-item label="搜索：">
        <el-input size="small" v-model="formInline.deptName" placeholder="输入商品名称"></el-input>
      </el-form-item>
      <el-form-item label="">
        <el-input size="small" v-model="formInline.deptNo" placeholder="输入商品代码"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button size="small" type="primary" icon="el-icon-search" @click="search">搜索</el-button>
        <el-button size="small" type="primary" icon="el-icon-plus" @click="handleAddGoods()">添加</el-button>
        <el-button size="small" type="danger" icon="el-icon-delete" @click="handleEdit()">批量删除</el-button>

      </el-form-item>
    </el-form>
    <!--列表-->
    <el-table size="small" :data="listData" highlight-current-row v-loading="loading" border
      element-loading-text="拼命加载中" style="width: 100%;">
      <el-table-column align="center" type="selection" width="60" />
      <el-table-column label="序号" width="50px" align="center">
        <template slot-scope="scope">
          {{ scope.$index + (formInline.current - 1) * formInline.size + 1 }}
        </template>
      </el-table-column>
      <el-table-column label="商品图片" align="center" width="120px">
        <template slot-scope="scope">
          <span v-for="(imgItem, imgIndex) in scope.row.srcList">
            <img :src="imgItem" alt="" style="width: 80px;height: 80px">
          </span>
        </template>
      </el-table-column>
      <el-table-column align="center" prop="goodsName" min-width="140" label="商品名称" />
      <el-table-column sortable prop="editUser" label="当前价" width="120" />
      <el-table-column sortable prop="editUser" label="库存" width="120" />
      <el-table-column sortable prop="editUser" label="30天销量" width="120" />
      <el-table-column sortable prop="createTime" label="修改时间" width="120">
        <template slot-scope="scope">
          <div>{{ scope.row.createTime | timestampToTime }}</div>
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作" min-width="300">
        <template slot-scope="scope">
          <el-button @click="handleEdit(scope.$index, scope.row)" type="primary" size="mini" icon="el-icon-edit">编辑
          </el-button>
          <el-button @click="handleEdit(scope.$index, scope.row)" type="success" size="mini" icon="el-icon-search">下架
          </el-button>
          <el-button @click="deleteUser(scope.$index, scope.row)" type="danger" size="mini" icon="el-icon-delete">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <Pagination v-bind:child-msg="pageparm" @callFather="callFather"></Pagination>
  </div>
</template>

<script>
import { goodsList, getImg } from '../../api/EcommerceManagement'
import Pagination from '../../components/Pagination'
export default {
  data() {
    return {
      fileList: [],
      ruleForm: {
        name: '',
        image: '',
        summary: '',
        issue: '',
      },
      loading: false, //是显示加载
      title: '添加',
      formInline: {
        current: 1,
        size: 10,
      },
      listData: [], //用户数据
      // 分页参数
      pageparm: {
        currentPage: 1,
        pageSize: 10,
        total: 10
      }
    }
  },
  // 注册组件
  components: {
    Pagination
  },
  /**
   * 创建完毕
   */
  created() {
    this.getdata(this.formInline)
  },

  /**
   * 里面的方法只有被调用才会执行
   */
  methods: {
    // 获取商品列表
    getdata(parameter) {
      this.loading = true
      this.pageparm.currentPage = this.formInline.current
      this.pageparm.pageSize = this.formInline.size
      goodsList(parameter).then(res => {
        this.loading = false
        if (res.success == false) {
          this.$message({
            type: 'info',
            message: res.msg
          })
        } else {
          // 分页赋值
          this.loading = false
          for (let item of res.records) {
            item.srcList = [];
            let imgPahtLlist = item.sliderImage.split(',')
            let zhutu = imgPahtLlist[0];
            let imgparam = {
              imgPath: zhutu
            }
            getImg(imgparam).then(res => {
              let blob = new Blob([res.data], { type: 'image/jpeg' });
              const imageUrl = URL.createObjectURL(blob);
              item.srcList.push(imageUrl);
            })
          }
          this.listData = res.records
          this.pageparm.currentPage = this.formInline.current
          this.pageparm.pageSize = this.formInline.size
          this.pageparm.total = res.count
        }
      }).catch(err => {
        this.loading = false
        this.$message.error('菜单加载失败，请稍后再试！')
      })
    },
    // 分页插件事件
    callFather(parm) {
      this.formInline.page = parm.currentPage
      this.formInline.limit = parm.pageSize
      this.getdata(this.formInline)
    },
    // 搜索事件
    search() {
    },
    //添加商品
    handleAddGoods: function(){
      this.$router.push({path:'/commerce/GoodsDetails'});
    },
    //跳转详情
    handleEdit: function (index, row) {
      console.log("商品ID" + row.id)
      this.$router.push({path:'/commerce/GoodsDetails',query:{id: row.id}});
    },
    // 删除商品
    deleteUser(index, row) {
      this.$confirm('确定要删除吗?', '信息', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          deptDelete(row.deptId)
            .then(res => {
              if (res.success) {
                this.$message({
                  type: 'success',
                  message: '公司已删除!'
                })
                this.getdata(this.formInline)
              } else {
                this.$message({
                  type: 'info',
                  message: res.msg
                })
              }
            })
            .catch(err => {
              this.loading = false
              this.$message.error('公司删除失败，请稍后再试！')
            })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    }
  }
}
</script>

<style scoped>
.user-search {
  margin-top: 20px;
}

.userRole {
  width: 100%;
}
</style>

 
 